<?php

class Admin_PhotoController extends Zend_Controller_Action
{
	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}

    public function init()
    {
        /* Initialize action controller here */
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->_model = new Default_Model_DbTable_Photo();
    	$this->view->fullName = $infoUser->USERNAME;
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle(photo_index_head,"PREPEND");
        $this->view->title = photo_index_head;
        $photo = $this->_model->GetAll();
        $this->view->allPhoto = $photo;
        $paginator = Zend_Paginator::factory($photo);
        $paginator->setItemCountPerPage(9);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam("page",1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->allPhoto = $paginator;
    }

    public function addAction(){
    	$this->view->headTitle(photo_add_head,"PREPEND");
    	$this->view->title = photo_add_head;
    	$upload = new Private_Upload();
    	$libPhoto = new Admin_Model_DbTable_LibraryPhoto();
    	$this->view->LibPhoto = $libPhoto->getAllLibPhoto();
    	
    	if($this->getRequest()->isPost()){
    		//$photodata = $this->_request->getPost();
    		$lpID = $this->_request->getParam("PhotoLibID");
    		$NameP = $this->_request->getParam("NamePhoto");
    		$stt = $this->_request->getParam("stt");
    		if($_FILES['PImg']['name']==null && $this->_request->getParam("textImg") == ""){
    			$img = "/ishali/public/uploaded/images/no_image.jpg";
    			$this->_model->AddPhoto($lpID, $NameP, $img, $stt);
    			$this->_redirect("/admin/photo");
    		}elseif($this->_request->getParam("textImg")){
    			$img = $this->_request->getParam("textImg");
    			$this->_model->AddPhoto($lpID, $NameP, $img, $stt);
    			$this->_redirect("/admin/photo");
    		}
    		else{
    			$img = "/ishali/public/uploaded/images/photo/".$_FILES['PImg']["name"];
    			$upload->upload("photo");
    			$this->_model->AddPhoto($lpID, $NameP, $img, $stt);
    			$this->_redirect("/admin/photo");
    		}
    	}
    }
    
    public function editAction(){
    	$this->view->headTitle("Chá»‰nh Sá»­a HÃ¬nh áº¢nh","PREPEND");
    	$this->view->title = "Chá»‰nh Sá»­a HÃ¬nh áº¢nh";
    	$upload = new Private_Upload();
    	$id = $this->_getParam("id");
    	$photo = $this->_model->GetByID($id);
    	$this->view->photo = $photo;
    	$libPhoto = new Admin_Model_DbTable_LibraryPhoto();
    	$this->view->LibPhoto = $libPhoto->getAllLibPhoto();
    	
    	if($this->getRequest()->isPost()){
    		$photodata = $this->_request->getPost();
    		$lpID = $this->_request->getParam("PhotoLibID");
    		$NameP = $this->_request->getParam("NamePhoto");
    		$stt = $this->_request->getParam("stt");
    		if($_FILES['PImg']['name']==null){
    			$img = $photo->PHOTO_IMG;
    			$this->_model->UpdatePhoto($id,$lpID, $NameP, $img, $stt);
    			$this->_redirect("/admin/photo");
    		}elseif($this->_request->getParam("textImg")->isValid){
    			$img = $this->_request->getParam("textImg");
    			$this->_model->AddPhoto($lpID, $NameP, $img, $stt);
    			$this->_redirect("/admin/photo");
    		}else{
    			$img = "/ishali/public/uploaded/images/photo/".$_FILES['PImg']["name"];
    			$upload->upload("photo");
    			$this->_model->UpdatePhoto($id,$lpID, $NameP, $img, $stt);
    			$this->_redirect("/admin/photo");
    		}
    	}
    }
    
    public function delAction(){
    	$id = $this->_getParam("id");
    	$this->_model->DelPhoto($id);
    	$this->_redirect("/admin/photo");
    }
    
    public function activeAction(){
    	$id = $this->_getParam("id");
    	$this->_model->Active($id);
    	$this->_redirect("/admin/photo");
    }

}

